package com.tang.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tang.dto.ImagesDto;
import com.tang.sys.ResponseResult;
import com.tang.sys.entity.Images;
import com.tang.sys.entity.Link;
import com.tang.sys.mapper.ImagesMapper;
import com.tang.sys.service.IImagesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tang.sys.vo.ImageTitleVo;
import com.tang.sys.vo.PageVo;
import com.tang.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shankaka
 * @since 2023-04-27
 */
@Service
public class ImagesServiceImpl extends ServiceImpl<ImagesMapper, Images> implements IImagesService {
    @Autowired
    private IImagesService iImagesService;

    @Resource
    private ImagesMapper imagesMapper;
    @Override
    public ResponseResult getImageTitle() {
        Images image = iImagesService.getById(1);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("id", image.getId());
        resultMap.put("imageUrl", image.getImageUrl());
        resultMap.put("imageTitle", image.getImageTitle());
        return ResponseResult.okResult(resultMap);
    }

    //获取全部的图片和title
    @Override
    public ResponseResult getAllImage() {
        List<Images> list = iImagesService.list();
        List<ImageTitleVo> imageTitleVos = BeanCopyUtils.copyBeanList(list, ImageTitleVo.class);

        return ResponseResult.okResult(imageTitleVos);
    }

    @Override
    public ResponseResult getAllList(Integer pageNum, Integer pageSize, ImagesDto imagesDto) {
        LambdaQueryWrapper<Images> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(imagesDto.getImageTitle()),Images::getImageTitle,imagesDto.getImageTitle());

        Page<Images> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,wrapper);

        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addImage(Images images) {
        imagesMapper.insert(images);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteImage(Long id) {
        LambdaQueryWrapper<Images> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Images::getId,id);
        this.imagesMapper.delete(wrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getImageInfo(Long id) {
        Images byId = iImagesService.getById(id);

        return ResponseResult.okResult(byId);
    }

    @Override
    public ResponseResult updateImage(Images images) {
        Images byId = iImagesService.getById(images);
        byId.setImageName(images.getImageName());
        byId.setImageTitle(images.getImageTitle());
        byId.setImageUrl(images.getImageUrl());

        imagesMapper.updateById(byId);
        return ResponseResult.okResult();
    }


}
